/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Server.ImplementInterface;

import DAL.DBManager;
import Server.DTO.DailyReportDetail_DTO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

/**
 *
 * @author 10520_000
 */
public class DailyReportDetailRepository {
    public static int Insert(DailyReportDetail_DTO info) {
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String query = "INSERT INTO DAILYREPORTDETAIL(DetailID, ReportID, TypeID, TotalOfRevenues, TotalOfCost) VALUES(?,?,?,?,?)";
            PreparedStatement pst = con.prepareStatement(query);
            
            pst.setString(1, info.getDetailID());
            pst.setString(2, info.getReportID());
            pst.setString(3, info.getSavingTypeID());
            pst.setInt(4, info.getTotalOfRevenues());
            pst.setInt(5, info.getTotalOfRevenues());
            
            iRet = pst.executeUpdate();
            pst.close();
            
        } catch (SQLException e) {
            System.out.println(e);
        }
        
        return iRet;
    }
    
    public static int Update(DailyReportDetail_DTO info) {
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String query = "UPDATE DAILYREPORTDETAIL SET ReportID= ?, TypeID= ?, TotalOfRevenues=?, TotalOfCost= ? WHERE DetailID= ?";
            PreparedStatement pst = con.prepareStatement(query);
            
            pst.setString(1, info.getDetailID());
            pst.setString(2, info.getReportID());
            pst.setString(3, info.getSavingTypeID());
            pst.setInt(4, info.getTotalOfRevenues());
            pst.setInt(5, info.getTotalOfRevenues());
            
            iRet = pst.executeUpdate();
            pst.close();
            
        } catch (SQLException e) {
            System.out.println(e);
        }
        
        return iRet;
    }
    
    public static int Delete(DailyReportDetail_DTO info) {
        int iRet = -1;
        try {
            Connection con = DBManager.getInstance().getConnection();
            String SQL = "DELETE FROM DAILYREPORTDETAIL WHERE DetailID=?";
            PreparedStatement pstmt = con.prepareStatement(SQL);
            pstmt.setString(1, info.getDetailID());

            iRet = pstmt.executeUpdate();

            pstmt.close();
        } catch (SQLException se) {
            System.out.println(se);
        }
        return iRet;
    }
    
    public static ArrayList FindAll() {
        ArrayList arr = new ArrayList();
        try {
            Connection con = DBManager.getInstance().getConnection();
            String sql = "SELECT * FROM DAILYREPORTDETAIL ORDER BY DetailID";
            Statement state = con.createStatement();
            ResultSet rs = state.executeQuery(sql);
            
            while(rs.next()){
                DailyReportDetail_DTO info = new DailyReportDetail_DTO();
                info.setDetailID(rs.getString("DetailID"));
                info.setReportID(rs.getString("ReportID"));
                info.setSavingTypeID(rs.getString("SavingTypeID"));
                info.setTotalOfCost(rs.getInt("TotalOfCost"));
                info.setTotalOfRevenues(rs.getInt("TotalOfRevenues"));
                arr.add(info);
            }
            state.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
        return arr;        
    }
    
    public static ArrayList FindByID(String DetailID) {
        ArrayList arr = new ArrayList();
        try {
            Connection con = DBManager.getInstance().getConnection();
            String query = "SELECT * FROM DAILYREPORTDETAIL WHERE DetailID LIKE(?) ORDER BY DetailID";
            PreparedStatement pst = con.prepareStatement(query);
            pst.setString(1, "%" + DetailID + "%");
            ResultSet rs = pst.executeQuery();
            
            while(rs.next()) {
                DailyReportDetail_DTO info = new DailyReportDetail_DTO();
                info.setDetailID(rs.getString("DeTailID"));
                info.setReportID(rs.getString("ReportID"));
                info.setSavingTypeID(rs.getString("SavingTypeID"));
                info.setTotalOfCost(rs.getInt("TotalOfCost"));
                info.setTotalOfRevenues(rs.getInt("TotalOfRevenues"));
                arr.add(info);
            }
            pst.close();
        } catch (SQLException e) {
            System.out.println(e);
        }
        return arr;
    }
}
